<?php

function dbCsvCompare($filePath) {
   if (!$finp = fopen($filePath, 'rb')) {
      return "impossibile aprire il file ".$filePath."\n";
   }

   $imgdb = array();
   $imgind = array();
   $imgtab = array();
   while ($ac = fgetcsv($finp,4048,';','"')) {
   	while(count($ac)<8) $ac[]='';
      if ($ac[1] == 'tab') {
         $imgtab[$ac[0]]['comment'] = trim($ac[7]);
      } elseif ($ac[1] == 'col') {
         
   	  	if (trim($ac[3]) == 'blob' or trim($ac[3]) == 'longblob'
          or trim($ac[3]) == 'text' or trim($ac[3]) == 'longtext'
          or preg_match('/auto_increment/', trim($ac[6]))) {
     		$ac[5] = "" ;
      	} else {
      		if (trim($ac[5]) == '') {        	
	        	if (trim($ac[3]) == 'date') {
		        	$ac[5] = "0000-00-00";
		        } elseif(trim($ac[3]) == 'datetime') {
		        	$ac[5] = "0000-00-00 00:00:00";
		        } elseif(substr(trim($ac[3]),0,3) == 'int') {
		        	$ac[5] = "0" ;			        		
		        } elseif(substr(trim($ac[3]),0,7) == 'decimal') {
		        	$ac[5] = "0.00" ;			        		
		        } elseif(trim($ac[3]) == 'char(1)') {
	        		$ac[5] = "." ;			        			        		        
		        } else {
		        	$ac[5] = "" ;
		        }
         	} 

      	}     
         
         $imgdb[$ac[0]][$ac[2]]['tipo'] = trim($ac[3]);
         $imgdb[$ac[0]][$ac[2]]['null'] = trim($ac[4]);         
         $imgdb[$ac[0]][$ac[2]]['default'] = trim($ac[5]);
         $imgdb[$ac[0]][$ac[2]]['extra'] = trim($ac[6]);
         $imgdb[$ac[0]][$ac[2]]['comment'] = trim($ac[7]);
      
      
      } elseif ($ac[1] == 'ind') {
         $imgind[$ac[0]][$ac[2]]['tipo'] = trim($ac[5]);
         $imgind[$ac[0]][$ac[2]][$ac[3]] = trim($ac[4]);
      }
   }
   fclose($finp);
	$risp = "";
   @mysql_connect(RGL_DB_HOST, RGL_DB_USER, RGL_DB_PWD);
   if (mysql_errno() > 0) {
      rglErr('('.RGL_DB_HOST.'/'.RGL_DB_USER.') errore '.mysql_errno()." - ".mysql_error());
   }
   mysql_select_db(RGL_DB_NAME);
   if (mysql_errno() > 0) {
      rglErr(mysql_errno()." = ".mysql_error());
   }
   $sql = "SHOW TABLE STATUS";
   if (!$qryt = mysql_query($sql)) {
      rglErr($sql . '<br>' . mysql_error());
   }
   $setdb = array();
   $setind = array();
   $settab = array();
   while ($rect = mysql_fetch_assoc($qryt)) {
      $tab = $rect['Name'];
      if (substr($rect['Comment'],0,6)== "InnoDB") $aX[0] = "";
      else $aX = explode("; InnoDB free:",$rect['Comment']);     
      $settab[$tab]['comment'] = $aX[0];
      $sql = "SHOW FULL COLUMNS FROM $tab";
      if (!$qryc = mysql_query($sql)) {
         rglErr($sql . '<br>' . mysql_error());
      } 
      while ($recc = mysql_fetch_assoc($qryc)) {      	
      	$type = $recc['Type'];      	
      	if ($recc['Default'] == null and 
      	   	  	!($type == 'blob' or $type == 'longblob'
               or $type == 'text' or $type == 'longtext'
               or preg_match('/auto_increment/', trim($recc['Extra'])))) {
               	$recc['Default'] = "";//"*@*NULL*@*";
        }      	
      	 
         if (substr($type,0,6)=='varbin') {
            $type = 'varchar'.substr($type,6).' binary';
         } elseif (isset($recc['Collation']) and $recc['Collation'] == 'latin1_bin') {
				if (substr($type,-7) != ' binary') {
					$type .= ' binary';
				}
         }
         $setdb[$tab][$recc['Field']]['tipo'] = trim($type);
         $setdb[$tab][$recc['Field']]['null'] = (trim($recc['Null']) == 'YES')?'YES':'';
         $setdb[$tab][$recc['Field']]['default'] = trim($recc['Default']);
         $setdb[$tab][$recc['Field']]['extra'] = trim($recc['Extra']);
         $setdb[$tab][$recc['Field']]['comment'] = trim($recc['Comment']);
      }

      $sql = "SHOW INDEX FROM $tab";
      if (!$qryc = mysql_query($sql)) {
         rglErr($sql . '<br>' . mysql_error());
      } while ($recc = mysql_fetch_array($qryc)) {
   		// la versione 4 di mysql ha il campo index_type
         // il campo  Comment  della ver. 3
         // e' sostituito dal  Index_type  della ver.4
         if ($recc['Index_type'] == 'FULLTEXT') {
            $setind[$tab][$recc['Key_name']]['tipo'] = trim($recc['Index_type']);
         } else {
            $setind[$tab][$recc['Key_name']]['tipo'] = trim($recc['Comment']);
         }
         $setind[$tab][$recc['Key_name']][$recc['Seq_in_index']] = trim($recc['Column_name']);
      }
   }

   foreach($imgdb as $tab => $aimg) {
      if (!isset($setdb[$tab])) {
         $risp .= "<br>Tabella $tab da creare<br>";
         foreach($aimg as $field => $aX) {
            $risp .= "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;campo $field: ";
            foreach($aX as $prop => $val) {
               if ($val != '') $risp .= $prop . '=' . $val . ' ';
            }
            $risp .= "<br>";
         }
         continue;
      }
      $aset = $setdb[$tab];
      ksort($aset);
      ksort($aimg);
      $serset = serialize($aset);
      $serimg = serialize($aimg);
      
      $aimgind = $imgind[$tab];
      $asetind = $setind[$tab];
      if (!empty($asetind)) ksort($asetind);
      if (!empty($aimgind)) ksort($aimgind);
      $sersetind = serialize($asetind);
      $serimgind = serialize($aimgind);
      
      $aimgtab = $imgtab[$tab];
      $asettab = $settab[$tab];
      if (!empty($asettab)) ksort($asettab);
      if (!empty($aimgtab)) ksort($aimgtab);
      $sersettab = serialize($asettab);
      $serimgtab = serialize($aimgtab);

      if ($serset == $serimg and $sersetind == $serimgind and $sersettab == $serimgtab) {
         continue;
      }
      $diff = 0;
      $risp .= "<br>Tabella $tab ha differenze<br>";
      if($sersettab != $serimgtab) {
         $diff ++;
         $risp .= "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;modifica commento<br>";
      }
      foreach($aset as $field => $aX) {
         if (!isset($aimg[$field])) {
            $diff ++;
            $risp .= "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;non previsto campo $field<br>";
         }
      }
      foreach($aimg as $field => $aX) {
         if (!isset($aset[$field])) {
            $diff++;
            $risp .= "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;da aggiungere campo $field: ";
            foreach($aX as $prop => $val) {
               if ($val != '') $risp .= $prop . '=' . $val . ' ';
            }
            $risp .= "<br>";
            continue;
         }
         $ay = $aset[$field];
         if (serialize($aX) != serialize($ay)) {         	
         	$diff ++;
            $risp .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;da modificare campo $field: ";
            foreach($ay as $prop => $val) {
               if ($val != '') $risp .= $prop . '=' . $val . ' ';
            }
            $risp .= "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in $field: ";
            foreach($aX as $prop => $val) {
               if ($val != '') $risp .= $prop . '=' . $val . ' ';
            }
            $risp .= "<br>";
         }
      }
      // indici cancellati
      // (non cancella la primary key se auto_increment))
      foreach($setind[$tab] as $nome => $ai) {
         if ($nome == 'PRIMARY') {
            if ($aset[$ai[1]]['extra'] == 'auto_increment') {
               continue;
            }
         }
         if (isset($imgind[$tab][$nome])) {
            continue;
         }
         $risp .= "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indice $nome non previsto<br>";
      }
      // confronta indici e cancella/crea indici modificati/nuovi
      // (non crea la primary key se auto_increment))
      foreach($imgind[$tab] as $nome => $ai) {
         if ($nome == 'PRIMARY') {
            if ($aset[$ai[1]]['extra'] == 'auto_increment') {
               continue;
            }
         }
         if (serialize($ai) == serialize($setind[$tab][$nome])) {
            continue;
         }
         if (isset($setind[$tab][$nome])) {
            $risp .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indice $nome da modificare";
         } else {
            $risp .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indice $nome da creare";
         }
         $lista = '';
         foreach($ai as $seq => $col) {
            if ($seq != 'tipo') {
               $lista .= $col . ',';
            }
         }
         $lista = substr($lista, 0, -1);

         $risp .= " ($lista)<br>";
      }
   }

   foreach($setdb as $tab => $aset) {
      if (!isset($imgdb[$tab])) {
      $risp .= "<br>Tabella $tab non utilizzata da EPS<br>";
      }
   }

   if (empty($risp)) return "Nessuna differenza riscontrata.";
   return $risp;
}

function dbCsvUpdate($filePath) {
   if (!$finp = fopen($filePath, 'rb')) {
      return "impossibile aprire il file ".$filePath."\n";
   }


   $imgdb = array();
   $imgind = array();
   $imgtab = array();
   while ($ac = fgetcsv($finp,4048,';','"')) {
   	while(count($ac)<8) $ac[]='';
      if ($ac[1] == 'tab') {
         $imgtab[$ac[0]]['comment'] = trim($ac[7]);
      } elseif ($ac[1] == 'col') {

      	 if (trim($ac[3]) == 'blob' or trim($ac[3]) == 'longblob'
          or trim($ac[3]) == 'text' or trim($ac[3]) == 'longtext'
          or preg_match('/auto_increment/', trim($ac[6]))) {
     		$ac[5] = "" ;
      	 } else {
      	   if (trim($ac[5]) == '') {        	
	        	if (trim($ac[3]) == 'date') {
		        	$ac[5] = "0000-00-00";
		        } elseif(trim($ac[3]) == 'datetime') {
		        	$ac[5] = "0000-00-00 00:00:00";
		        } elseif(substr(trim($ac[3]),0,3) == 'int') {
		        	$ac[5] = "0" ;			        		
		        } elseif(substr(trim($ac[3]),0,7) == 'decimal') {
		        	$ac[5] = "0.00" ;			        		
		        } elseif(trim($ac[3]) == 'char(1)') {
	        		$ac[5] = "." ;			        			        
		        } else {
		        	$ac[5] = "" ;
		        }
         	} 
      	 }      
      	
      	$imgdb[$ac[0]][$ac[2]]['tipo'] = trim($ac[3]);
         $imgdb[$ac[0]][$ac[2]]['null'] = trim($ac[4]);
         $imgdb[$ac[0]][$ac[2]]['default'] = trim($ac[5]);
         $imgdb[$ac[0]][$ac[2]]['extra'] = trim($ac[6]);
         $imgdb[$ac[0]][$ac[2]]['comment'] = trim($ac[7]);
      } elseif ($ac[1] == 'ind') {
         $imgind[$ac[0]][$ac[2]]['tipo'] = trim($ac[5]);
         $imgind[$ac[0]][$ac[2]][$ac[3]] = trim($ac[4]);
      }
   }
   fclose($finp);

	$risp = "";
   @mysql_connect(RGL_DB_HOST, RGL_DB_USER, RGL_DB_PWD);
   if (mysql_errno() > 0) {
      rglErr('('.RGL_DB_HOST.'/'.RGL_DB_USER.') errore '.mysql_errno()." - ".mysql_error());
   }
   mysql_select_db(RGL_DB_NAME);
   if (mysql_errno() > 0) {
      rglErr(mysql_errno()." = ".mysql_error());
   }
   $sql = "SHOW TABLE STATUS";
   if (!$qryt = mysql_query($sql)) {
      rglErr($sql . '<br>' . mysql_error());
   }
   $setdb = array();
   $setind = array();
   $settab = array();
   while ($rect = mysql_fetch_assoc($qryt)) {
      $tab = $rect['Name'];
      if (substr($rect['Comment'],0,6)== "InnoDB") $aX[0] = "";
      else $aX = explode("; InnoDB free:",$rect['Comment']);     
      $settab[$tab]['comment'] = $aX[0];
      $sql = "SHOW FULL COLUMNS FROM $tab";
      if (!$qryc = mysql_query($sql)) {
         rglErr($sql . '<br>' . mysql_error());
      } 
      while ($recc = mysql_fetch_assoc($qryc)) {
      	$type = $recc['Type'];
      	if ($recc['Default'] == null and 
      	    !($type == 'blob' or $type == 'longblob'
           or $type == 'text' or $type == 'longtext'
           or preg_match('/auto_increment/', trim($recc['Extra'])))) {
      		$recc['Default'] = ""; //"*@*NULL*@*";
      	}      	
        
        if (substr($type,0,6)=='varbin') {
            $type = 'varchar'.substr($type,6).' binary';
         } elseif (isset($recc['Collation']) and $recc['Collation'] == 'latin1_bin') {
				if (substr($type,-7) != ' binary') {
					$type .= ' binary';
				}
         }
         $setdb[$tab][$recc['Field']]['tipo'] = trim($type);
         $setdb[$tab][$recc['Field']]['null'] = (trim($recc['Null']) == 'YES')?'YES':'';
         $setdb[$tab][$recc['Field']]['default'] = trim($recc['Default']);
         $setdb[$tab][$recc['Field']]['extra'] = trim($recc['Extra']);
         $setdb[$tab][$recc['Field']]['comment'] = trim($recc['Comment']);
      }

      $sql = "SHOW INDEX FROM $tab";
      if (!$qryc = mysql_query($sql)) {
         rglErr($sql . '<br>' . mysql_error());
      } 
      while ($recc = mysql_fetch_array($qryc)) {
   		// la versione 4 di mysql ha il campo index_type
         // il campo  Comment  della ver. 3
         // sostituito dal  Index_type  della ver.4
         if ($recc['Index_type'] == 'FULLTEXT') {
            $setind[$tab][$recc['Key_name']]['tipo'] = trim($recc['Index_type']);
         } else {
            $setind[$tab][$recc['Key_name']]['tipo'] = trim($recc['Comment']);
         }
         $setind[$tab][$recc['Key_name']][$recc['Seq_in_index']] = trim($recc['Column_name']);
      }
   }

   foreach($imgdb as $tab => $aimg) {
      if (!isset($setdb[$tab])) {
         $risp .= "Crea tabella $tab<br>";
         crea_tabella($tab, $aimg, $imgind[$tab]);
         continue;
      }
      $aset = $setdb[$tab];
      $aimgS = $aimg;
      ksort($aset);
      ksort($aimgS);
      $serset = serialize($aset);
      $serimg = serialize($aimgS);
      
      $aimgind = $imgind[$tab];
      $asetind = $setind[$tab];
      if (!empty($asetind)) ksort($asetind);
      if (!empty($aimgind)) ksort($aimgind);
      $sersetind = serialize($asetind);
      $serimgind = serialize($aimgind);
      
      $aimgtab = $imgtab[$tab];
      $asettab = $settab[$tab];
      if (!empty($asettab)) ksort($asettab);
      if (!empty($aimgtab)) ksort($aimgtab);
      $sersettab = serialize($asettab);
      $serimgtab = serialize($aimgtab);

      if ($serset == $serimg and $sersetind == $serimgind and $sersettab == $serimgtab) {
         continue;
      }
      //echo $tab."-----------------<br>\r\n";
      //echo "-db--".$serset."<br>\r\n";
      //echo "-img-".$serimg."<br>\r\n";
      $risp .= "Modifica tabella $tab<br>";
      if($sersettab != $serimgtab) {
         $sql = "ALTER TABLE $tab COMMENT '".addslashes($aimgtab['comment'])."' ";
         if (!mysql_query($sql)) {
            rglErr($sql . '<br>' . mysql_error());
         }
      }
      /*
      // non elimina o modifica indici esistenti
      // (non cancella la primary key se auto_increment))
      foreach($setind[$tab] as $nome => $ai) {
         if ($nome == 'PRIMARY') {
            if ($aset[$ai[1]]['extra'] == 'auto_increment') {
               continue;
            }
         }
         if (isset($imgind[$tab][$nome])
         and serialize($ai) == serialize($imgind[$tab][$nome])) {
            continue;
         }
         if ($nome == 'PRIMARY') {
            $sql = "ALTER TABLE $tab DROP PRIMARY KEY";
         } else {
            $sql = "ALTER TABLE $tab DROP INDEX " . $nome . "";
         }
         if (!mysql_query($sql)) {
            rglErr($sql . '<br>' . mysql_error());
         }
      }
      //*/
      // modifica campi
      foreach($aset as $field => $aX) {
         if (!isset($aimg[$field])) {
            //elimina_campo($tab, $field);
         }
      }
      $pri_img = '';
      $pri_set = '';
      $ind_add = array();
      $ind_del = array();
      foreach($aimg as $field => $aX) {
         if (!isset($aset[$field])) {
            aggiungi_campo($tab, $field, $aX);
            continue;
         }
         $ay = $aset[$field];
         if (serialize($aX) != serialize($ay)) {
            modifica_campo($tab, $field, $aX);
         }
      }
      // confronta indici e crea indici nuovi
      // non modifica indici esistenti
      // (non crea la primary key se auto_increment))
      foreach($imgind[$tab] as $nome => $ai) {
         if ($nome == 'PRIMARY') {
            if ($aimg[$ai[1]]['extra'] == 'auto_increment') {
               continue;
            }
         }
         /*
         if (isset($setind[$tab][$nome])
         and serialize($ai) == serialize($setind[$tab][$nome])) {
               continue;
         }
         //*/
         // crea solo indici non modifica gli esistenti
         if (isset($setind[$tab][$nome])) {
               continue;
         }
         $lista = '';
         foreach($ai as $seq => $col) {
            if ($seq != 'tipo') {
               $lista .= $col . ',';
            }
         }
         $lista = substr($lista, 0, -1);

         if ($nome == 'PRIMARY') {
            $sql = "ALTER TABLE $tab ADD PRIMARY KEY (" . $lista . ")";
         } elseif ($ai['tipo'] == 'FULLTEXT') {
            $sql = "ALTER TABLE $tab ADD FULLTEXT " . $nome . " (" . $lista . ")";
         } else {
            $sql = "ALTER TABLE $tab ADD INDEX " . $nome . " (" . $lista . ")";
         }
         if (!mysql_query($sql)) {
            //print_r($aset); print_r($ai); die(" *** $nome ***");
            rglErr($sql . '<br>' . mysql_error());
         }
      }
   }
   if (empty($risp)) return "Nessuna differenza riscontrata.";
   return $risp;
}

function crea_tabella($tab, $aimg, $aind)
{
   $str = '';
   // carica campi primary key
   $aPKfield = array();
   foreach($aind as $nome => $ai) {
      if ($nome == 'PRIMARY') {
      	foreach($ai as $kY => $fY) {
         	if ($kY>0) $aPKfield[] = $fY;
      	}
      }
   }
   $ind_add = array();
   $bPKautoincr = false;
   foreach($aimg as $field => $aX) {
      $str .= $field . " " . $aX['tipo'];
      if ($aX['null'] == 'YES') $str .= ' NULL';
      else $str .= ' NOT NULL';
      if ($aX['default'] != '') $str .= " DEFAULT '" . $aX['default'] . "'";
      if (preg_match('/auto_increment/', $aX['extra'])) {
         $str .= " PRIMARY KEY AUTO_INCREMENT";
         $bPKautoincr = true;
      } 
      
      $str .= " COMMENT '".addslashes($aX['comment'])."' , ";
   }
   if (!$bPKautoincr && count($aPKfield)>0) {
   	$str .= " PRIMARY KEY (".implode(',',$aPKfield).")   ";
   }
   $sql = "CREATE TABLE $tab (" . substr($str, 0, -2) . ")";
   if (!mysql_query($sql)) {
      rglErr($sql . '<br>' . mysql_error());
   }
   // crea indici
   foreach($aind as $nome => $ai) {
      if ($nome == 'PRIMARY') {
         continue;
      }
      $lista = '';
      foreach($ai as $seq => $col) {
         if ($seq != 'tipo') {
            $lista .= $col . ',';
         }
      }
      $lista = substr($lista, 0, -1);
		if ($ai['tipo'] == 'FULLTEXT') {
         $sql = "ALTER TABLE $tab ADD FULLTEXT " . $nome . " (" . $lista . ")";
      } else {
         $sql = "ALTER TABLE $tab ADD INDEX " . $nome . " (" . $lista . ")";
      }
      if (!mysql_query($sql)) {
         rglErr($sql . '<br>' . mysql_error());
      }
   }
}

function elimina_campo($tab, $field)
{
   $sql = "ALTER TABLE $tab DROP $field";
   if (!mysql_query($sql)) {
      rglErr($sql . '<br>' . mysql_error());
   }
}

function aggiungi_campo($tab, $field, $aX)
{
   set_time_limit(60);
	
   $sql = "ALTER TABLE $tab ADD $field" . " " . $aX['tipo'];
   if ($aX['null'] == 'YES') $sql .= ' NULL';
   else $sql .= ' NOT NULL';

   if ($aX['default'] == "*@*NULL*@*") {
       $aX['default'] = "";
   }	
     
   if ($aX['default'] == '') {        	
        if ($aX['tipo'] == 'date') {
	        $sql .= " DEFAULT '0000-00-00'";
	    } elseif($aX['tipo'] == 'datetime') {
	        $sql .= " DEFAULT '0000-00-00 00:00:00'";
	    } elseif(substr($aX['tipo'],0,3) == 'int') {
	        if (!preg_match('/auto_increment/', $aX['extra'])) {
	        	$sql .= " DEFAULT '0'" ;			        		
	        }
	    } elseif(substr($aX['tipo'],0,7) == 'decimal') {
	        if (!preg_match('/auto_increment/', $aX['extra'])) {
	        	$sql .= " DEFAULT '0.00'" ;			        		
	        }
	    } elseif($aX['tipo'] == 'char(1)') {
	        $sql .= " DEFAULT ' '" ;			        			        
	    } else {
	        $sql .= " DEFAULT ''" ;
	    }
   } else {
      	$sql .= " DEFAULT '" . $aX['default'] . "'";
   }
          
// if ($aX['default'] != '') $sql .= " DEFAULT '" . $aX['default'] . "'";
   if (preg_match('/auto_increment/', $aX['extra'])) $sql .= " PRIMARY KEY AUTO_INCREMENT";
   $sql .= " COMMENT '".addslashes($aX['comment'])."' "; 
   if (!mysql_query($sql)) {
      rglErr($sql . '<br>' . mysql_error());
   }
}

function modifica_campo($tab, $field, $aX)
{
   set_time_limit(60);
	
   $sql = "ALTER TABLE $tab CHANGE $field $field" . " " . $aX['tipo'];
   if ($aX['null'] == 'YES') $sql .= " NULL";
   else $sql .= " NOT NULL";
   if ($aX['tipo'] != 'blob' and $aX['tipo'] != 'longblob'
      and $aX['tipo'] != 'text' and $aX['tipo'] != 'longtext') {
        if ($aX['default'] == "*@*NULL*@*") { 
      		$aX['default'] = '';
        }	
        if ($aX['default'] == '') {        	
        	if ($aX['tipo'] == 'date') {
	        	$sql .= " DEFAULT '0000-00-00'";
	        } elseif($aX['tipo'] == 'datetime') {
	        	$sql .= " DEFAULT '0000-00-00 00:00:00'";
	        } elseif(substr($aX['tipo'],0,3) == 'int') {
	        	if (!preg_match('/auto_increment/', $aX['extra'])) {
	        		$sql .= " DEFAULT '0'" ;			        		
	        	}
	        } elseif(substr($aX['tipo'],0,7) == 'decimal') {
	        	if (!preg_match('/auto_increment/', $aX['extra'])) {
	        		$sql .= " DEFAULT '0.00'" ;			        		
	        	}
	        } elseif($aX['tipo'] == 'char(1)') {
	        		$sql .= " DEFAULT ' '" ;			        			        
	        } else {
	        	$sql .= " DEFAULT ''" ;
	        }
        } else {
      		$sql .= " DEFAULT '" . $aX['default'] . "'";
        }
   }
   if (preg_match('/auto_increment/', $aX['extra'])) $sql .= " AUTO_INCREMENT";
   $sql .= " COMMENT '".addslashes($aX['comment'])."' "; 
   
   if (!mysql_query($sql)) {
      rglErr($sql . '<br>' . mysql_error());
   }
   
   /*
   if ($aX['tipo'] != 'blob' and $aX['tipo'] != 'longblob'
      and $aX['tipo'] != 'text' and $aX['tipo'] != 'longtext') {
      if ($aX['default'] == '') {
         $sql = "ALTER TABLE $tab ALTER $field DROP DEFAULT";
			if (!mysql_query($sql)) {
				rglErr($sql . '<br>' . mysql_error());
			}	
      }
   }*/      
}

